home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / util / gnu / a2_0bEmacs_bin.lha / Emacs-19.25 / lisp / amiga-menu-bar.el < prev    next >
Lisp/Scheme  |  1994-08-28  |  16KB  |  436 lines

  1. ;;; amiga-menu-bar.el --- define a default menu bar.
  2. ;;; it's nearly menu-bar.el except multi-frame stuff (CHFIXME)
  3.  
  4. (defun frame-list () "Dummy frame-list, returns list of selected- frame."
  5.   (list (selected-frame))) ; CHFIXME
  6.  
  7. ;;; menu-bar.el --- define a default menu bar.
  8.  
  9. ;; Author: RMS
  10. ;; Keywords: internal
  11.  
  12. ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
  13.  
  14. ;; This file is part of GNU Emacs.
  15.  
  16. ;; GNU Emacs is free software; you can redistribute it and/or modify
  17. ;; it under the terms of the GNU General Public License as published by
  18. ;; the Free Software Foundation; either version 2, or (at your option)
  19. ;; any later version.
  20.  
  21. ;; GNU Emacs is distributed in the hope that it will be useful,
  22. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24. ;; GNU General Public License for more details.
  25.  
  26. ;; You should have received a copy of the GNU General Public License
  27. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  28. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  
  30. ;;; Code:
  31.  
  32. ;; Don't clobber an existing menu-bar keymap, to preserve any menu-bar key
  33. ;; definitions made in loaddefs.el.
  34. (or (lookup-key global-map [menu-bar])
  35.     (define-key global-map [menu-bar] (make-sparse-keymap "menu-bar")))
  36. (defvar menu-bar-help-menu (make-sparse-keymap "Help"))
  37. ;; Put Help item last.
  38. (setq menu-bar-final-items '(help))
  39. (define-key global-map [menu-bar help] (cons "Help" menu-bar-help-menu))
  40. (defvar menu-bar-edit-menu (make-sparse-keymap "Edit"))
  41. (define-key global-map [menu-bar edit] (cons "Edit" menu-bar-edit-menu))
  42. (defvar menu-bar-file-menu (make-sparse-keymap "File"))
  43. (define-key global-map [menu-bar file] (cons "File" menu-bar-file-menu))
  44.  
  45. (define-key menu-bar-file-menu [exit-emacs]
  46.   '("Exit Emacs" . save-buffers-kill-emacs))
  47. (define-key menu-bar-file-menu [kill-buffer]
  48.   '("Kill Buffer" . kill-this-buffer))
  49. ;; (define-key menu-bar-file-menu [delete-frame] '("Delete Frame" . delete-frame)) 
  50. (define-key menu-bar-file-menu [epatch]
  51.   '("Apply Patch" . menu-bar-epatch-menu))
  52. (define-key menu-bar-file-menu [ediff]
  53.   '("Compare Files" . menu-bar-ediff-menu))
  54. (define-key menu-bar-file-menu [emerge] '("Emerge" . menu-bar-emerge-menu))
  55. (define-key menu-bar-file-menu [calendar] '("Calendar" . calendar))
  56. (define-key menu-bar-file-menu [rmail] '("Read Mail" . rmail))
  57. (define-key menu-bar-file-menu [gnus] '("Read Net News" . gnus))
  58. (define-key menu-bar-file-menu [bookmark]
  59.   '("Bookmarks" . menu-bar-bookmark-map))
  60. (define-key menu-bar-file-menu [print-buffer] '("Print Buffer" . print-buffer))
  61. (define-key menu-bar-file-menu [revert-buffer]
  62.   '("Revert Buffer" . revert-buffer))
  63. (define-key menu-bar-file-menu [write-file]
  64.   '("Save Buffer As..." . write-file))
  65. (define-key menu-bar-file-menu [save-buffer] '("Save Buffer" . save-buffer))
  66. (define-key menu-bar-file-menu [dired] '("Open Directory..." . dired))
  67. (define-key menu-bar-file-menu [open-file] '("Open File..." . find-file))
  68. ;; (define-key menu-bar-file-menu [make-frame] '("Make New Frame" . make-frame))
  69.  
  70. (define-key menu-bar-edit-menu [spell] '("Spell" . ispell-menu-map))
  71. (define-key menu-bar-edit-menu [fill] '("Fill" . fill-region))
  72. (define-key menu-bar-edit-menu [clear] '("Clear" . delete-region))
  73. (define-key menu-bar-edit-menu [query-replace]
  74.   '("Query Replace" . query-replace))
  75. (define-key menu-bar-edit-menu [re-search-back]
  76.   '("Regexp Search Backwards" . re-search-backward))
  77. (define-key menu-bar-edit-menu [search-back]
  78.   '("Search Backwards" . search-backward))
  79. (define-key menu-bar-edit-menu [re-search-fwd]
  80.   '("Regexp Search" . re-search-forward))
  81. (define-key menu-bar-edit-menu [search-fwd]
  82.   '("Search" . search-forward))
  83. (define-key menu-bar-edit-menu [choose-next-paste]
  84.   '("Choose Next Paste >" . mouse-menu-choose-yank))
  85. (define-key menu-bar-edit-menu [paste] '("Paste" . yank))
  86. (define-key menu-bar-edit-menu [copy] '("Copy" . kill-ring-save))
  87. (define-key menu-bar-edit-menu [cut] '("Cut" . kill-region))
  88. (define-key menu-bar-edit-menu [undo] '("Undo" . undo))
  89.  
  90. (put 'fill-region 'menu-enable 'mark-active)
  91. (put 'kill-region 'menu-enable 'mark-active)
  92. (put 'kill-ring-save 'menu-enable 'mark-active)
  93. (put 'yank 'menu-enable '(x-selection-exists-p))
  94. (put 'delete-region 'menu-enable 'mark-active)
  95. (put 'undo 'menu-enable '(if (eq last-command 'undo)
  96.                  pending-undo-list
  97.                (consp buffer-undo-list)))
  98. (put 'query-replace 'menu-enable (not buffer-read-only))
  99.  
  100. (autoload 'ispell-menu-map "ispell" nil t 'keymap)
  101.  
  102. ;; These are alternative definitions for the cut, paste and copy
  103. ;; menu items.  Use them if your system expects these to use the clipboard
  104.  
  105. (put 'clipboard-kill-region 'menu-enable 'mark-active)
  106. (put 'clipboard-kill-ring-save 'menu-enable 'mark-active)
  107. (put 'clipboard-yank 'menu-enable
  108.      '(or (x-selection-exists-p) (x-selection-exists-p 'CLIPBOARD)))
  109.  
  110. (defun clipboard-yank ()
  111.   "Reinsert the last stretch of killed text, or the clipboard contents."
  112.   (interactive)
  113.   (let ((x-select-enable-clipboard t))
  114.     (yank)))
  115.  
  116. (defun clipboard-kill-ring-save (beg end)
  117.   "Copy region to kill ring, and save in the X clipboard."
  118.   (interactive "r")
  119.   (let ((x-select-enable-clipboard t))
  120.     (kill-ring-save beg end)))
  121.  
  122. (defun clipboard-kill-region (beg end)
  123.   "Kill the region, and save it in the X clipboard."
  124.   (interactive "r")
  125.   (let ((x-select-enable-clipboard t))
  126.     (kill-region beg end)))
  127.  
  128. (defun menu-bar-enable-clipboard ()
  129.   "Make the menu bar CUT, PASTE and COPY items use the clipboard."
  130.   (interactive)
  131.   ;; We can't use constant list structure here because it becomes pure,
  132.   ;; and because it gets modified with cache data.
  133.   (define-key menu-bar-edit-menu [paste]
  134.     (cons "Paste" 'clipboard-yank))
  135.   (define-key menu-bar-edit-menu [copy]
  136.     (cons "Copy" 'clipboard-kill-ring-save))
  137.   (define-key menu-bar-edit-menu [cut]
  138.     (cons "Cut" 'clipboard-kill-region)))
  139.  
  140. ;; Sun expects these commands on these keys, so why not?
  141. (define-key global-map [f20] 'clipboard-kill-region)
  142. (define-key global-map [f16] 'clipboard-kill-ring-save)
  143. (define-key global-map [f18] 'clipboard-yank)
  144.  
  145. (define-key menu-bar-help-menu [emacs-version]
  146.   '("Show Version" . emacs-version))
  147. (define-key menu-bar-help-menu [report-emacs-bug]
  148.   '("Send Bug Report" . report-emacs-bug))
  149. (define-key menu-bar-help-menu [emacs-tutorial]
  150.   '("Emacs Tutorial" . help-with-tutorial))
  151. (define-key menu-bar-help-menu [man] '("Man..." . manual-entry))
  152. (define-key menu-bar-help-menu [describe-variable]
  153.   '("Describe Variable..." . describe-variable))
  154. (define-key menu-bar-help-menu [describe-function]
  155.   '("Describe Function..." . describe-function))
  156. (define-key menu-bar-help-menu [describe-key]
  157.   '("Describe Key..." . describe-key))
  158. (define-key menu-bar-help-menu [list-keybindings]
  159.   '("List Keybindings" . describe-bindings))
  160. (define-key menu-bar-help-menu [command-apropos]
  161.   '("Command Apropos..." . command-apropos))
  162. (define-key menu-bar-help-menu [describe-mode]
  163.   '("Describe Mode" . describe-mode))
  164. (define-key menu-bar-help-menu [info] '("Info" . info))
  165.  
  166. (define-key menu-bar-help-menu [emacs-news] '("Emacs News" . view-emacs-news))
  167. (defun kill-this-buffer ()    ; for the menubar
  168.   "Kills the current buffer."
  169.   (interactive)
  170.   (kill-buffer (current-buffer)))
  171.  
  172. (defun kill-this-buffer-enabled-p ()
  173.   (let ((count 0)
  174.     (buffers (buffer-list)))
  175.     (while buffers
  176.       (or (string-match "^ " (buffer-name (car buffers)))
  177.       (setq count (1+ count)))
  178.       (setq buffers (cdr buffers)))
  179.     (> count 1)))
  180.  
  181. (put 'save-buffer 'menu-enable '(buffer-modified-p))
  182. (put 'revert-buffer 'menu-enable
  183.      '(or revert-buffer-function revert-buffer-insert-file-contents-function
  184.       (and (buffer-file-name)
  185.            (or (buffer-modified-p)
  186.            (not (verify-visited-file-modtime (current-buffer)))))))
  187. ;; Permit deleting frame if it would leave a visible or iconified frame.
  188. ;(put 'delete-frame 'menu-enable
  189. ;     '(let ((frames (frame-list))
  190. ;        (count 0))
  191. ;    (while frames
  192. ;      (if (cdr (assq 'visibility (frame-parameters (car frames))))
  193. ;          (setq count (1+ count)))
  194. ;      (setq frames (cdr frames)))
  195. ;    (> count 1)))
  196. (put 'kill-this-buffer 'menu-enable '(kill-this-buffer-enabled-p))
  197.  
  198. (put 'advertised-undo 'menu-enable
  199.      '(and (not (eq t buffer-undo-list))
  200.        (if (eq last-command 'undo)
  201.            (and (boundp 'pending-undo-list)
  202.             pending-undo-list)
  203.          buffer-undo-list)))
  204.  
  205. (defvar yank-menu-length 100
  206.   "*Maximum length of an item in the menu for \
  207. \\[mouse-menu-choose-yank].")
  208.  
  209. (defun mouse-menu-choose-yank (event)
  210.   "Pop up a menu of the kill-ring for selection with the mouse.
  211. The kill-ring-yank-pointer is moved to the selected element.
  212. A subsequent \\[yank] yanks the choice just selected."
  213.   (interactive "e")
  214.   (let* ((count 0)
  215.      (menu (mapcar (lambda (string)
  216.              (if (> (length string) yank-menu-length)
  217.                  (setq string (substring string
  218.                              0 yank-menu-length)))
  219.              (prog1 (cons string count)
  220.                (setq count (1+ count))))
  221.                kill-ring))
  222.      (arg (x-popup-menu event 
  223.                 (list "Yank Menu"
  224.                   (cons "Choose Next Yank" menu)))))
  225.     ;; A mouse click outside the menu returns nil.
  226.     ;; Avoid a confusing error from passing nil to rotate-yank-pointer.
  227.     ;; XXX should this perhaps do something other than simply return? -rm
  228.     (if arg
  229.     (progn
  230.       ;; We don't use `rotate-yank-pointer' because we want to move
  231.       ;; relative to the beginning of kill-ring, not the current
  232.       ;; position.  Also, that would ask for any new X selection and
  233.       ;; thus change the list of items the user just chose from, which
  234.       ;; would be highly confusing.
  235.       (setq kill-ring-yank-pointer (nthcdr arg kill-ring))
  236.       (if (interactive-p)
  237.           (message "The next yank will insert the selected text.")
  238.         (current-kill 0))))))
  239. (put 'mouse-menu-choose-yank 'menu-enable 'kill-ring)
  240.  
  241. (define-key global-map [menu-bar buffer] '("Buffers" . menu-bar-buffers))
  242.  
  243. (defalias 'menu-bar-buffers (make-sparse-keymap "Buffers"))
  244.  
  245. (defvar complex-buffers-menu-p nil
  246.   "*Non-nil says, offer a choice of actions after you pick a buffer.
  247. This applies to the Buffers menu from the menu bar.")
  248.  
  249. (defvar buffers-menu-max-size 10
  250.   "*Maximum number of entries which may appear on the Buffers menu.
  251. If this is 10, then only the ten most-recently-selected buffers are shown.
  252. If this is nil, then all buffers are shown.
  253. A large number or nil slows down menu responsiveness.")
  254.  
  255. (defvar list-buffers-directory nil)
  256.  
  257. (defun menu-bar-select-buffer ()
  258.   (interactive)
  259.   (switch-to-buffer last-command-event))
  260.  
  261. ;(defun menu-bar-select-frame ()
  262. ;  (interactive)
  263. ;  (make-frame-visible last-command-event)
  264. ;  (raise-frame last-command-event)
  265. ;  (select-frame last-command-event))
  266.  
  267. (defvar menu-bar-update-buffers-last-buffers nil)
  268. (defvar menu-bar-update-buffers-last-frames nil)
  269.  
  270. (defun menu-bar-update-buffers ()
  271.   (let ((buffers (buffer-list))
  272.     (frames (frame-list))
  273.     buffers-info
  274.     buffers-menu frames-menu)
  275.     (setq buffers-info
  276.       (mapcar (function (lambda (buffer)
  277.                   (list buffer (buffer-modified-p buffer)
  278.                     (save-excursion
  279.                       (set-buffer buffer)
  280.                       buffer-read-only))))
  281.           buffers))
  282.     (if (and (equal buffers-info menu-bar-update-buffers-last-buffers)
  283.          (equal frames menu-bar-update-buffers-last-frames))
  284.     nil
  285.       (setq menu-bar-update-buffers-last-buffers buffers-info)
  286.       (setq menu-bar-update-buffers-last-frames frames)
  287.       ;; If requested, list only the N most recently selected buffers.
  288.       (if (and (integerp buffers-menu-max-size)
  289.            (> buffers-menu-max-size 1))
  290.       (if (> (length buffers) buffers-menu-max-size)
  291.           (setcdr (nthcdr buffers-menu-max-size buffers) nil)))
  292.  
  293.       ;; Make the menu of buffers proper.
  294.       (setq buffers-menu
  295.         (cons "Select Buffer"
  296.           (let ((tail buffers)
  297.             (maxbuf 0)
  298.             (maxlen 0)
  299.             alist
  300.             head)
  301.             (while tail
  302.               (or (eq ?\ (aref (buffer-name (car tail)) 0))
  303.               (setq maxbuf
  304.                 (max maxbuf
  305.                      (length (buffer-name (car tail))))))
  306.               (setq tail (cdr tail)))
  307.             (setq tail buffers)
  308.             (while tail
  309.               (let ((elt (car tail)))
  310.             (or (eq ?\ (aref (buffer-name elt) 0))
  311.                 (setq alist (cons
  312.                      (cons
  313.                       (format
  314.                        (format "%%%ds  %%s%%s  %%s"
  315.                            maxbuf)
  316.                        (buffer-name elt)
  317.                        (if (buffer-modified-p elt)
  318.                            "*" " ")
  319.                        (save-excursion
  320.                          (set-buffer elt)
  321.                          (if buffer-read-only "%" " "))
  322.                        (or (buffer-file-name elt)
  323.                            (save-excursion
  324.                          (set-buffer elt)
  325.                          list-buffers-directory)
  326.                            ""))
  327.                       elt)
  328.                      alist)))
  329.             (and alist (> (length (car (car alist))) maxlen)
  330.                  (setq maxlen (length (car (car alist))))))
  331.               (setq tail (cdr tail)))
  332.             (setq alist (nreverse alist))
  333.             (nconc (mapcar '(lambda (pair)
  334.                       ;; This is somewhat risque, to use
  335.                       ;; the buffer name itself as the event type
  336.                       ;; to define, but it works.
  337.                       ;; It would not work to use the buffer
  338.                       ;; since a buffer as an event has its
  339.                       ;; own meaning.
  340.                       (nconc (list (buffer-name (cdr pair))
  341.                            (car pair)
  342.                            (cons nil nil))
  343.                          'menu-bar-select-buffer))
  344.                    alist)
  345.                (list (cons 'list-buffers
  346.                        (cons
  347.                     (concat (make-string (max (- (/ maxlen
  348.                                     2)
  349.                                      8)
  350.                                   0) ?\ )
  351.                         "List All Buffers")
  352.                     'list-buffers)))))))
  353.  
  354.  
  355.       ;; Make a Frames menu if we have more than one frame.
  356.       (if (cdr frames)
  357.       (setq frames-menu
  358.         (cons "Select Frame"
  359.               (mapcar '(lambda (frame)
  360.                  (nconc (list frame
  361.                           (cdr (assq 'name
  362.                              (frame-parameters frame)))
  363.                           (cons nil nil))
  364.                     'menu-bar-select-frame))
  365.                   frames))))
  366.       (if buffers-menu
  367.       (setq buffers-menu (cons 'keymap buffers-menu)))
  368.       (if frames-menu
  369.       (setq frames-menu (cons 'keymap frames-menu)))
  370.       (define-key global-map [menu-bar buffer]
  371.     (cons "Buffers"
  372.           (if (and buffers-menu frames-menu)
  373.           (list 'keymap "Buffers and Frames"
  374.             (cons 'buffers (cons "Buffers" buffers-menu))
  375.             (cons 'frames (cons "Frames" frames-menu)))
  376.         (or buffers-menu frames-menu 'undefined)))))))
  377.  
  378. (add-hook 'menu-bar-update-hook 'menu-bar-update-buffers)
  379.  
  380. ;; this version is too slow
  381. ;;;(defun format-buffers-menu-line (buffer)
  382. ;;;  "Returns a string to represent the given buffer in the Buffer menu.
  383. ;;;nil means the buffer shouldn't be listed.  You can redefine this."
  384. ;;;  (if (string-match "\\` " (buffer-name buffer))
  385. ;;;      nil
  386. ;;;    (save-excursion
  387. ;;;     (set-buffer buffer)
  388. ;;;     (let ((size (buffer-size)))
  389. ;;;       (format "%s%s %-19s %6s %-15s %s"
  390. ;;;           (if (buffer-modified-p) "*" " ")
  391. ;;;           (if buffer-read-only "%" " ")
  392. ;;;           (buffer-name)
  393. ;;;           size
  394. ;;;           mode-name
  395. ;;;           (or (buffer-file-name) ""))))))
  396.  
  397. (defun menu-bar-mode (flag)
  398.   "Toggle display of a menu bar on each frame.
  399. This command applies to all frames that exist and frames to be
  400. created in the future.
  401. With a numeric argument, if the argument is negative,
  402. turn off menu bars; otherwise, turn on menu bars."
  403.  (interactive "P")
  404.  
  405.  ;; Obtain the current setting by looking at default-frame-alist.
  406.  (let ((menu-bar-mode
  407.     (not (zerop (let ((assq (assq 'menu-bar-lines default-frame-alist)))
  408.               (if assq (cdr assq) 0))))))
  409.  
  410.    ;; Tweedle it according to the argument.
  411.    (setq menu-bar-mode (if (null flag) (not menu-bar-mode)
  412.              (> (prefix-numeric-value flag) 0)))
  413.  
  414.    ;; Apply it to default-frame-alist.
  415.    (let ((parameter (assq 'menu-bar-lines default-frame-alist)))
  416.      (if (consp parameter)
  417.      (setcdr parameter (if menu-bar-mode 1 0))
  418.        (setq default-frame-alist
  419.          (cons (cons 'menu-bar-lines (if menu-bar-mode 1 0))
  420.            default-frame-alist))))
  421.  
  422.    ;; Apply it to existing frames.
  423.    (let ((frames (frame-list)))
  424.      (while frames
  425.        (let ((height (cdr (assq 'height (frame-parameters (car frames))))))
  426.      (modify-frame-parameters (car frames)
  427.                   (list (cons 'menu-bar-lines
  428.                         (if menu-bar-mode 1 0))))
  429.      (modify-frame-parameters (car frames)
  430.                   (list (cons 'height height))))
  431.        (setq frames (cdr frames))))))
  432.  
  433. (provide 'menu-bar)
  434.  
  435. ;;; menu-bar.el ends here
  436.